// pages/goods_mall/payResult.js
let status = '';
var App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderMoney:0,
    orderNo:'123456789',
    payResultTip:'',
    text:'',
    orderId:'',
    adImgUrl:'',
    needToken:false, //是否需要token
    adGoUrl:'', //跳转到url
    jifen:'0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    if (options.type =='replenishment'){
      status = 'replenishment';
      this.setData({
        payResultTip:'补货成功',
        text:'囤货商品总金额'
      })
    }else{
      status = 'xiaofei';
      this.setData({
        payResultTip: '支付成功',
        text: '支付金额'
      })
      
    }
    this.setData({
      orderMoney: parseFloat(options.price).toFixed(2),
      orderNo: options.order_no,
      orderId: options.order_id,
      jifen: options.jifen
    })
    
    if (status == 'replenishment') {//补货
      this.getBanner()
    } else (
      this.getXiaofeiBanner()
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // var pages = getCurrentPages();
    // var prePage = pages[pages.length - 2];//触发上个界面的方法
    // if (prePage.route.indexOf('/index') > -1) { //补货非首页的时候
    //   wx.redirectTo({
    //     url: '/pages/index/index',
    //   })
    // }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //返回首页
  onBackPage:function(){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  //前往消费
  go_goods_mall:function(){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  //查看订单
  goOrderDetail:function(){
    if (status == 'replenishment'){
      wx.navigateTo({
        url: '/pages/mine/order/order_detail?type=replenishment&order_no=' + this.data.orderNo+'&type=0',
      })
    }else{
      wx.navigateTo({
        url: '/pages/mine/order/purchase_detail?order_no=' + this.data.orderNo,
      })
    }
  },

  getBanner: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/sideshow/banner/get',
      data: {
        type: 5,
        userId: wx.getStorageSync('userInfo').uid,
        schoolId: wx.getStorageSync('userInfo').school_id == null ? '' : wx.getStorageSync('userInfo').school_id,
        cityId: wx.getStorageSync('userInfo').city_id == null ? '' : wx.getStorageSync('userInfo').city_id,
        token: wx.getStorageSync('token'),
        support: 1 //0: 全部 1: 小程序 2: 安卓 3: IOS
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) { 
          if(res.data.data.length>0){
            that.setData({
              adImgUrl: res.data.data[0].img,
              needToken: res.data.data[0].use_token=='0'?false:true,
              adGoUrl: res.data.data[0].url
            })
          }   
        } else {//登录过期
          wx.showToast({
            title: res.data.error,
            icon:'none'
          })
        }
      }
    })
  },

  getXiaofeiBanner: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/sideshow/banner/get',
      data: {
        type: 6,
        userId: wx.getStorageSync('userInfo').uid,
        schoolId: wx.getStorageSync('userInfo').school_id == null ? '' : wx.getStorageSync('userInfo').school_id,
        cityId: wx.getStorageSync('userInfo').city_id == null ? '' : wx.getStorageSync('userInfo').city_id,
        token: wx.getStorageSync('token'),
        support: 1 //0: 全部 1: 小程序 2: 安卓 3: IOS
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          if (res.data.data.length > 0) {
            that.setData({
              adImgUrl: res.data.data[0].img,
              needToken: res.data.data[0].use_token == '0' ? false : true,
              adGoUrl: res.data.data[0].url
            })
          }
        } else {//登录过期
          wx.showToast({
            title: res.data.error,
            icon: 'none'
          })
        }
        wx.hideLoading();
      }
    })
  },


  // 活动广告页跳转
  resultAdUrl:function(){
    if (this.data.adGoUrl != ''){//
      var url = this.data.adGoUrl + '&token=' + wx.getStorageSync('token') + '&openId=' + wx.getStorageSync('userInfo').openid;
      
      if (this.data.adGoUrl.indexOf('?') > -1 && this.data.needToken){ //返回跳转链接有？
        wx.navigateTo({
          url: '/pages/index/active_page?url=' + encodeURIComponent(url),
        })
      } else if (this.data.adGoUrl.indexOf('?') == -1 && this.data.needToken){
        wx.navigateTo({
          url: '/pages/index/active_page?url=' + encodeURIComponent(url),
        })
      }else{
        wx.navigateTo({
          url: '/pages/index/active_page?url=' + encodeURIComponent(this.data.adGoUrl)
        })
      }
    }
  }
})